package com.hzit.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.ClassesService;
import com.hzit.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
	@Autowired
	private ClassesService classesService;

	//1. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pageSize, Student student) {
		//1.1 封装查询条件
		QueryWrapper<Student> wrapper = new QueryWrapper<>() ;
		if(student != null){
			wrapper.like(StringUtils.isNotBlank(student.getSname()),"sname",student.getSname());
			wrapper.like(StringUtils.isNotBlank(student.getAddr()),"addr",student.getAddr());
			wrapper.eq(student.getCid() != null && student.getCid() != 0,"cid",student.getCid());
		}

		//1.2 开始分页查询
		Page<Student> studentPage = this.page(new Page<>(page, pageSize), wrapper);
		List<Student> records = studentPage.getRecords();
		for (Student stud : records) {
			Integer cid = stud.getCid();
			Classes classes = classesService.getById(cid);
			stud.setCname(classes.getCname());
		}

		//1.3 返回
		return new PageResult<>(studentPage.getTotal(),records);
	}
}
